//
//  NSString+Custom.m
//  AFNetworking
//
//  Created by Huadong Zheng on 2023/4/11.
//

#import "NSString+Custom.h"
#import "UIColor+Custom.h"
#import "CommonDefine.h"
#import "UIFont+EasyAndLayout.h"

@implementation NSString (Custom)
+(BOOL)isAvailableString:(NSString *)string{
    if (!string || ![string isKindOfClass:[NSString class]] || [string length]==0) {
        return false;
    }
    return true;
}

/**
 返回一个可用的字符串

 @param string       原字符串
 @param defaultValue 默认字符串
 @return             有效的字符
 */
+(NSString *)getAvailableString:(NSString *)string defaultValue:(NSString *)defaultValue{
    return [self isAvailableString:string] ? string : defaultValue;
}

/**
 @desc      去除字符串开头指定的字符
 @author    Created by LeoHao on 2015-6-15
 */
- (NSString *)stringByTrimmingLeftCharactersInSet:(NSCharacterSet *)characterSet {
    NSUInteger location = 0;
    NSUInteger length = [self length];
    unichar charBuffer[length];
    [self getCharacters:charBuffer];
    
    
    return [self substringWithRange:NSMakeRange(location, length - location)];
}


/**
 @desc      去除字符串尾端指定的字符
 @author    Created by LeoHao on 2015-6-15
 */
- (NSString *)stringByTrimmingRightCharactersInSet:(NSCharacterSet *)characterSet {
    NSUInteger location = 0;
    NSUInteger length = [self length];
    unichar charBuffer[length];
    [self getCharacters:charBuffer];

    
    return [self substringWithRange:NSMakeRange(location, length - location)];
}

//字符串逆序
-(NSMutableString*)reverseString
{
    NSUInteger length = [self length];
    NSMutableArray *array = [NSMutableArray arrayWithCapacity:length];
    
    for(long i=length-1; i>=0; i--)
    {
        unichar c = [self characterAtIndex:i];
        [array addObject:[NSString stringWithFormat:@"%c",c]];
    }
    
    NSMutableString *str = [NSMutableString stringWithCapacity:length];
    for(int i=0; i<=length-1; i++)
    {
        [str appendString:array[i]];
    }
    return str;
}
-(NSString *) subStringFaceUserAtIndex:(NSInteger) index{
    
    NSRange range = NSMakeRange(0, 0);
    NSInteger zixingIndex = 0;
    for (NSInteger i = 0; i < self.length; i += range.length) {
        
        range = [self rangeOfComposedCharacterSequenceAtIndex:i];
        
        
        if(zixingIndex == index){
            return [self substringWithRange:range];
        }
        
        zixingIndex += 1;
    }
    return nil;
}

-(NSString *) subStringFaceUserFromRange:(NSRange) range{
    
    NSRange tmpRange = NSMakeRange(0, 0);
    
    NSInteger zixingIndex = 0;
    
    
    NSInteger zixingFromIndex = 0;
    NSInteger zixingToIndex = 0;
    
    for (NSInteger i = 0; i < self.length; i += tmpRange.length) {
        
        tmpRange = [self rangeOfComposedCharacterSequenceAtIndex:i];
        
        
        if(zixingIndex == range.location){
            zixingFromIndex = i;
        }
        if(zixingIndex == range.location + range.length){
            zixingToIndex = i; //不包括
        }
        
        zixingIndex += 1;
    }
    
    if(zixingFromIndex >= zixingToIndex){
        return nil;
    }
    
    
    
    return [self substringWithRange:(NSMakeRange(zixingFromIndex, zixingToIndex - zixingFromIndex))];
    
}
-(NSInteger) lengthFaceUser{
    NSInteger zixingIndex = 0;
    
    NSRange tmpRange = NSMakeRange(0, 0);
    
    for (NSInteger i = 0; i < self.length; i += tmpRange.length) {
        
        tmpRange = [self rangeOfComposedCharacterSequenceAtIndex:i];
        zixingIndex += 1;
    }
    return zixingIndex;
}

+(NSMutableAttributedString *)stringWithArray:(NSArray<NSString *> *)strs color:(NSArray *)colors font:(NSArray *)fonts{
    


    return [self stringWithArray:strs color:colors font:fonts expand:@{}];
}

+(NSMutableAttributedString *)stringWithArray:(NSArray<NSString *> *)strs color:(NSArray *)colors font:(NSArray *)fonts expand:(NSDictionary<NSNumber *, NSDictionary *> *) expandDicts{
    NSMutableAttributedString * mstr = [[NSMutableAttributedString alloc] init];
    for (NSInteger index = 0; index < strs.count; index += 1) {
        UIColor * color;
        if ([colors[index] isKindOfClass:[NSString class]]) {
            color = [UIColor colorWithHexString:colors[index]];
        }else{
            color = colors[index];
        }
        UIFont * font;
        if ([fonts[index] isKindOfClass:[NSNumber class]]) {
            font = F([fonts[index] floatValue]);

            
        }else{
            font = fonts[index];
        }
        
        NSDictionary * expandDict = expandDicts[@(index)];
        
        NSMutableDictionary * mDict = [NSMutableDictionary dictionaryWithDictionary:expandDict];
        mDict[NSFontAttributeName] = font;
        mDict[NSForegroundColorAttributeName] = color;
        
        NSAttributedString * str = [[NSAttributedString alloc] initWithString:strs[index] attributes:mDict];
        [mstr appendAttributedString:str];
    }

    return mstr;
}
@end
